<template>
	<div style="display: flex;flex-direction: column;height: 100%;">
		<div class="container" style="flex: 1;overflow-y: scroll;position: relative;">
			<div style="position: absolute;width: 100%;display: flex;flex-direction: column;">
				<image src="../../static/image/img_banner_3x.png" class="shadowback"
					style="width: 100%;border-radius: 0rpx 0rpx 32rpx 32rpx;">

				</image>

			</div>
			<div style="height: 420rpx;position: relative;margin-top: 96rpx;">
				<div class="shadowback"
					style="margin:0rpx 16rpx;position: absolute;bottom: 0;background: white;height: 220rpx;border-radius: 16rpx;left: 12rpx;right: 12rpx;">

					<div @click="clickUser"
						style="width: 400rpx;left: 0rpx;right: 0rpx;margin: 0 auto;position: absolute;top: -60rpx;display: flex;flex-direction: column;align-items: center;">
						<image :src="userInfo==null?'/static/icon/boy.png' : userInfo.avatarUrl"
							style="width: 120rpx;height: 120rpx;"></image>
						<span
							style="margin-top: 12rpx;font-size: 38rpx;font-weight: bolder;">{{loginInfo==null?'点击登录':loginInfo.nickname}}</span>
						<span style="margin-top: 12rpx;font-size: 24rpx;color:grey">已使用共
							<span>{{loginInfo==null?'--':getUseTime()}}</span>
							天</span>
					</div>
				</div>
			</div>

			<view
				style="background-color: white;margin: 32rpx;border-radius: 16rpx;display: flex;flex-direction: row;z-index: 999;flex-direction: column;"
				class="shadowback">

				<div style="margin-left: 32rpx;margin-right: 32rpx;display: flex;margin-top: 24rpx;">
					<div
						style="background-color: orange;height: 40rpx;width: 12rpx;border-radius: 16rpx;margin-right: 16rpx;">
					</div>
					开考学期
				</div>
				<div @click="showSelectSemester"
					style="height: 72rpx;background: #fef8e0;margin: 16rpx 32rpx;border-radius: 16rpx;display: flex;align-items: center;justify-content: space-between;padding: 0rpx 32rpx;">
					<div style="color: #ffaa00">{{selectSemester==null?'请选择':selectSemester.text}}</div>
					<fui-icon style="color: #ffaa00;" size="42" @click="leftClick" color="black"
						name="arrowright"></fui-icon>
				</div>

				<div style="margin-left: 32rpx;margin-right: 32rpx;display: flex;margin-top: 8rpx;">
					<div
						style="background-color: orange;height: 40rpx;width: 12rpx;border-radius: 16rpx;margin-right: 16rpx;">
					</div>
					开考科目
				</div>

				<div @click="showSelectSubject"
					style="height: 72rpx;background: #fef8e0;margin: 16rpx 32rpx;border-radius: 16rpx;display: flex;align-items: center;justify-content: space-between;padding: 0rpx 32rpx;">
					<div style="color: #ffaa00">{{selectSubject==null?'请选择':selectSubject.text}}</div>
					<fui-icon style="color: #ffaa00;" size="42" @click="leftClick" color="black"
						name="arrowright"></fui-icon>
				</div>
			</view>


			<view @click="gotoUserManage"
				style="background-color: white;height: 96rpx;margin: 32rpx;border-radius: 16rpx;display: flex;flex-direction: row;align-items: center;z-index: 999;"
				class="shadowback">
				<span style="margin-left: 32rpx;">用户管理</span>
				<div class="space"></div>
				<fui-icon style="margin-right: 16rpx;" size="48" name="arrowright"></fui-icon>
			</view>

			<view style="padding:0rpx 32rpx;box-sizing: border-box;margin-bottom: 32rpx;">
				<fui-button @click="loginOut" v-if="loginInfo!=null" type="danger">退出登录</fui-button>
			</view>
			
		</div>
		<fui-tabbar v-if="false" :isFixed="false" :tabBar="tabBar3" :current="current" @click="itemClick"></fui-tabbar>

		<U-TabBar :current="1"></U-TabBar>
	</div>

	<fui-picker linkage title="请选择学期" :layer="1" :options="semesterList" :show="selectSemesterShow"
		@change="selectSemesterChange" @cancel="onShowSelectSemesterCancel"></fui-picker>

	<fui-picker linkage title="请选择科目" :layer="1" :options="subjectList" :show="selectSubjectShow"
		@change="selectSubjectChange" @cancel="onShowSelectSubjectCancel"></fui-picker>

</template>

<script>
	// import Tabbar from '@/components/tabbar.vue'
	import request from "/request/request.js"
	import store from '/store/index.js'
	export default {
		components: {
			// Tabbar
		},
		data() {
			return {

				tabBar3: [{
						text: "首页",
						iconPath: "/static/icon/icon_home.png",
						selectedIconPath: "/static/icon/icon_home_fill.png"
					},
					{
						iconPath: "/static/icon/circle-active.png",
						midButton: true,
						width: 96,
						height: 96
					},
					{
						text: "我的",
						iconPath: "/static/icon/icon_my.png",
						selectedIconPath: "/static/icon/icon_my.png"
					},
				],
				loginInfo: null,

				selectSemesterShow: false,
				semesterList: [{
					text: '七年级上学期（第一学期）',
					value: 1
				}, {
					text: '七年级下学期（第二学期）',
					value: 2
				}, {
					text: '八年级上学期（第三学期）',
					value: 3
				}, {
					text: '八年级下学期（第四学期）',
					value: 4
				}, {
					text: '九年级上学期（第五学期）',
					value: 5
				}, {
					text: '九年级下学期（第六学期）',
					value: 6
				}],
				selectSemester: null,

				selectSubjectShow: false,
				subjectList: [],
				selectSubject: null,
			}
		},
		methods: {
			getLoginInfo() {
				console.log("getLoginInfo");
				let loginInfo = uni.getStorageSync('loginInfo');

				if (loginInfo != null && loginInfo != '') {
					this.loginInfo = JSON.parse(loginInfo);
				}

				console.log(this.loginInfo);

			},
			getUserInfo() {
				console.log("getUserInfo");
				let userInfo = uni.getStorageSync('userInfo');
				if (userInfo != null && userInfo != '') {
					this.userInfo = JSON.parse(userInfo);
				}
				console.log(this.userInfo);
			},
			gotoUserManage() {
				console.log("gotoUserManage");
				uni.navigateTo({
					url: '/pages/userManage/userManage'
				})
			},
			getUseTime() {
				var new_date = new Date();
				// var old_date = new Date("2018-12-12 00:00:00"); 
				var old_date = new Date(this.loginInfo.createTime);
				console.log("old_date:" + this.loginInfo.createTime);
				return parseInt((new_date - old_date) / 1000 / 3600 / 24);
			},
			clickUser() {
				if (this.loginInfo == null) {
					uni.navigateTo({
						url: '/pages/accountManage/accountManage'
					})
				} else {
					uni.navigateTo({
						url: '/pages/setting/setting'
					})
				}
			},
			weixinLogin() {
				let that = this;
				uni.showLoading({
					title: '加载中'
				});
				uni.getUserProfile({
					lang: 'zh_CN',
					desc: '用户登录', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，
					success: (res) => {
						console.log(res, 'resss')
						uni.setStorageSync('userInfo', JSON.stringify(res.userInfo));
						that.getUserInfo();
					},
					fail: (err) => {
						console.log(err, 'err')
					}
				})
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes);
						that.login(loginRes.code);
						uni.hideLoading();
					},
					fail: function(err) {
						// 登录授权失败  
						// err.code是错误码
						console.log(err);
						uni.hideLoading();
					}
				});
			},
			login(wxLoginCode) {
				let that = this;
				request({
					url: `/user/wxLogin?wxLoginCode=` + wxLoginCode,
					method: 'POST'
				}).then((res) => {
					if (res.code == 1008) {

						uni.navigateTo({
							url: '/pages/login/login?openId=' + res.data
						})
					}
					if (res.code == 0) {
						if (res.code == 0) {
							uni.setStorageSync('loginInfo', JSON.stringify(res.data));
							uni.showToast({
								title: "登录成功"
							});
							that.getLoginInfo();
						}
					}
					console.log(JSON.stringify(res));
				}, (err) => {
					console.log('fail' + err);
				});
			},
			loginOut() {
				uni.setStorage({
					key: 'loginInfo',
					data: null
				})
				uni.setStorage({
					key: 'userInfo',
					data: null
				})
				this.loginInfo = null;
				this.userInfo = null;
				console.log("loginOut");
			},
			showSelectSemester() {
				this.selectSemesterShow = true;
			},
			onShowSelectSemesterCancel(e) {
				this.selectSemesterShow = false;

			},
			selectSemesterChange(e) {
				let that = this;
				console.log(e);
				this.selectSemester = e;
				this.selectSemesterShow = false;
				this.selectSubject = null;
				store.setSemester(e);
				request({
					url: `/subject/getAllSubBySemester?semester=` + this.selectSemester.value,
					method: 'POST'
				}).then((res) => {
					console.log(res);

					that.subjectList = [];
					if (res.data.length == 0) {
						that.subjectList = [{
							text: ""
						}, ];
						return;
					}
					for (var i = 0; i < res.data.length; i++) {
						let obj = {
							text: res.data[i].name,
							value: res.data[i].id,
						}
						that.subjectList.push(obj);
					}
				}, (err) => {
					console.log('fail' + err);
				});
			},
			showSelectSubject() {
				this.selectSubjectShow = true;
			},
			onShowSelectSubjectCancel(e) {
				this.selectSubjectShow = false;

			},
			selectSubjectChange(e) {
				let that = this;
				console.log(e);
				store.setSubject(e);
				this.selectSubject = e;
				this.selectSubjectShow = false;

			}

		},
		onShow() {
			console.log("onShow");
			this.getLoginInfo();
			// uni.hideTabBar({
			// 	animation: false
			// })
		},
	}
</script>

<style scoped></style>